/**
 * 
 */
package com.broadtext.rms.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.rms.system.model.SysResource;
import com.broadtext.rms.system.service.ISysResourceService;

/**
 * @author guhuajie
 *
 */
@Service("sysResourceService")
public class SysResourceServiceImpl extends EntityServiceImpl<SysResource> implements ISysResourceService {

	@Autowired
    @Qualifier("sysResourceDao")
    public void setEntityDao(EntityDao<SysResource> entityDao) {
        super.entityDao = entityDao;
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<SysResource> getResourceTree(String type) throws DaoException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", type);
		return this.entityDao.findByQuery("com.broadtext.rms.system.model.SysResource.findResourceTreeByQuery", params);
	}

    @Override
    public void delSysResource(String[] resIdArray, String userName) throws DaoException {
        for (String id : resIdArray) {
            SysResource r;
            r = this.entityDao.findEntityById("com.broadtext.rms.system.model.SysResource.selectByPrimaryKey", id);
            if (r != null) {
                this.delResource(r, userName);
            }
        }
    }

    @SuppressWarnings("unchecked")
    private void delResource(SysResource sysResource, String userName) {
        String type;
        type = sysResource.getResType();
        try {
            if (!"3".equals(type)) {
                Map<String, Object> queryMap;
                queryMap = new HashMap<String, Object>();
                queryMap.put("parentId", sysResource.getPkId());
                List<SysResource> list;
                list = this.entityDao.findByQuery("com.broadtext.rms.system.model.SysResource.selectResourceByParentId", queryMap);
                for (SysResource res : list) {
                    this.delResource(res, userName);
                }
            }
            sysResource.setDelFlag("1");
            sysResource.setUpdateDate(new Date());
            sysResource.setUpdateUser(userName);
            this.entityDao.updateRecord("com.broadtext.rms.system.model.SysResource.updateByPrimaryKey", sysResource);
        } catch (DaoException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String updateSysResource(SysResource resource) throws DaoException {
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        
        //验证唯一性
        queryMap.put("resName", resource.getResName());
        queryMap.put("resUrl", resource.getResUrl());
        queryMap.put("id", resource.getPkId());
        if (!this.entityDao.findByQuery("com.broadtext.rms.system.model.SysResource.judgeUnique",
                queryMap).isEmpty()) {
            return "duplicate";
        }
        //修改的上级目录 是 原此目录的 子目录
        if (resource.getPkId().equals(resource.getParentId())) {
            return "parentId_error";
        }
        this.entityDao.updateRecord("com.broadtext.rms.system.model.SysResource.updateByPrimaryKey", resource);
        return "success";
    }

    @Override
    public String insertSysResource(SysResource resource) throws DaoException {
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        //验证唯一性
        queryMap.put("resName", resource.getResName());
        queryMap.put("resUrl", resource.getResUrl());
        if (!this.entityDao.findByQuery("com.broadtext.rms.system.model.SysResource.judgeUnique",queryMap).isEmpty()) {
            return "duplicate";
        }
        this.entityDao.insertRecord("com.broadtext.rms.system.model.SysResource.insertResource", resource);
        return "success";
    }
}
